package com.rocksdata.mc.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ctz.commonframe.ui.view.EmbeddedGridView;
import com.ctz.commonframe.utils.GlideApp;
import com.ctz.commonframe.utils.Logger;
import com.rocksdata.mc.R;
import com.rocksdata.mc.entity.NewsInfo2;

import java.util.List;

/**
 * Created by 陈堂正 on 2017/4/6.
 */

public class NewsListAdapter2 extends BaseAdapter {
    private Context context;
    private List<NewsInfo2> newsInfoList;
    private LayoutInflater inflater;

    public NewsListAdapter2(Context context, List<NewsInfo2> newsInfoList){
        this.context = context;
        this.newsInfoList = newsInfoList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        Logger.e("getCount:" + newsInfoList.size());
        return newsInfoList.size();
    }

    @Override
    public NewsInfo2 getItem(int position) {
        return newsInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final NewsInfo2 info = getItem(position);
        Logger.e("news code:" + info.getNewsCode());
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_news_list2, null);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_news_title);
            holder.ivImage = (ImageView) convertView.findViewById(R.id.iv_news_image);
            holder.gvImage = (EmbeddedGridView) convertView.findViewById(R.id.gv_news_image);
            holder.layoutVideo = (RelativeLayout) convertView.findViewById(R.id.layout_video_news);
            holder.ivVideoImage = (ImageView) convertView.findViewById(R.id.iv_news_video_image);
            holder.tvOtherInfo = (TextView) convertView.findViewById(R.id.tv_news_other_info);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvTitle.setText(info.getTitle());
        holder.tvOtherInfo.setText(info.getDepartmentName() + "  " + info.getPublishedTime());
        if(info.getVideoCoverPic() == null){
            if(info.getImgURL() == null || info.getImgURL().size() == 0){
                holder.layoutVideo.setVisibility(View.GONE);
                holder.ivImage.setVisibility(View.GONE);
                holder.gvImage.setVisibility(View.GONE);
            }else if(info.getImgURL().size() == 1){
                holder.layoutVideo.setVisibility(View.GONE);
                holder.ivImage.setVisibility(View.VISIBLE);
                holder.gvImage.setVisibility(View.GONE);
//                if(Config.isDebug){
//                    holder.ivImage.setImageResource(info.getImageList().get(0).getRes());
//                }else{
//                    XUtils3ImageDisplayer.display(holder.ivImage, info.getImageList().get(0).getImageUrl());
//                }

                GlideApp.with(context).load(info.getImgURL().get(0)).into(holder.ivImage);

            }else {
                holder.layoutVideo.setVisibility(View.GONE);
                holder.ivImage.setVisibility(View.GONE);
                holder.gvImage.setVisibility(View.VISIBLE);
                NewsImageGridAdapter adapter = new NewsImageGridAdapter(context, info.getImgURL());
                holder.gvImage.setAdapter(adapter);
                holder.gvImage.setClickable(false);
                holder.gvImage.setPressed(false);
                holder.gvImage.setEnabled(false);

            }
        }else{
            holder.layoutVideo.setVisibility(View.VISIBLE);
            holder.ivImage.setVisibility(View.GONE);
            holder.gvImage.setVisibility(View.GONE);
//            if(Config.isDebug){
//                holder.ivVideoImage.setImageResource(info.getVideoImage().getRes());
//            }else{
//                XUtils3ImageDisplayer.display(holder.ivImage, info.getVideoImage().getImageUrl());
//            }
            GlideApp.with(context).load(info.getVideoCoverPic()).into(holder.ivImage);
        }

        return convertView;
    }

    class ViewHolder {
        TextView tvTitle;
        ImageView ivImage;
        EmbeddedGridView gvImage;
        RelativeLayout layoutVideo;
        ImageView ivVideoImage;
        TextView tvOtherInfo;
    }
}
